#include <iostream>
#include <time.h>
#include <math.h>

#include "gfx.h"
#include "player.h"

using namespace std;

int audio_rate = 22050;
Uint16 audio_format = AUDIO_S16; /* 16-bit stereo */
int audio_channels = 2;
int audio_buffers = 4096;
Mix_Music *music = NULL;


enum states
{
        title,
        game,
        gameover,
} gamestate;

int speed = 10;

bool ai1 = false;
bool ai2 = true;

int sizeX = 800;
int sizeY = 400;

polaris::window *win;


void maingame();

void titlescreen()
{
	gamestate=title;

	polaris::player *titlescreen = new polaris::player();
	if(!titlescreen->loadimg("fgx/title.bmp")) exit(-1);
	win->setImg(titlescreen->gfx,0,0);
	win->refresh();

	while(true)
	{
		SDL_Delay(10);
		int poll = win->pollInputs();
		if(poll==QUIT) break;

		else if(poll==KSPACE)
		{
			maingame();
		}

	}
}

void score(int winner)
{
	gamestate=gameover;

	char *filename;

	switch (winner)
	{
		case 1: filename = "fgx/p1win.bmp"; break;
		case 2: filename = "fgx/p2win.bmp"; break;
		default: exit(-1); break;
	}

	polaris::player *titlescreen = new polaris::player();
	if(!titlescreen->loadimg(filename)) exit(-1);
	win->setImg(titlescreen->gfx,0,0);
	win->refresh();

	while(true)
	{
		SDL_Delay(30);
		int poll = win->pollInputs();
		if(poll==QUIT) break;

		else if(poll==KSPACE)
		{
			maingame();
		}

	}
}


void maingame()
{
	gamestate=game;

    	polaris::player *player1 = new polaris::player();
    	polaris::player *player2 = new polaris::player();

    	int ballcount = 20;
    	polaris::player *balls[ballcount];
    	for(int i=0; i<ballcount; i++)
    	{
       		balls[i] = new polaris::player();

       		balls[i]->posX =sizeX/2;
       		balls[i]->posY = sizeY/2;

       		balls[i]->forceX = 4; if(rand()%2==1) balls[i]->forceX *= -1;
       		balls[i]->forceY = 1+i%4;  if(rand()%2==1) balls[i]->forceY *= -1;

		balls[i]->score=10;

		if(balls[i]->loadimg("fgx/ball.bmp")) ; else exit(-1);

	}

	if(player1->loadimg("fgx/hero.bmp")) ; else exit(-1);
    	if(player2->loadimg("fgx/oponent.bmp")) ; else exit(-1);


	polaris::player *background = new polaris::player();
    	if(!background->loadimg("fgx/sea.bmp")) exit(-1);

	player1->posX = 10;
    	player1->posY = 50;

	player2->posX = sizeX-player2->w-40;
	player2->posY = 300;

	int backgroundshift = 0;	bool cdir = false;

	while (true)
	{
		SDL_Delay(10);
       		//win->clear();

		/* look for a winner */
		int lowerme = -1;
		for(int i=0; i<ballcount; i++)
		{
			lowerme = max(balls[i]->score, lowerme);
		}


		if(lowerme==-1)
		{
			cout << "GAME OVER" << endl;
			if(player1->score>=player2->score) score(1);
			else score(2);
		}

       		win->setImg(background->gfx, backgroundshift,(int)(sin(0.05*backgroundshift)*(background->h-sizeY)/2.3)-(background->h-sizeY)/2);
       		if(cdir)
       		{
       			if(backgroundshift<=0) backgroundshift+=1;
       			else cdir=false;
       		}
		else if(!cdir)
       		{
       			if(abs(backgroundshift)<abs(background->w-sizeX)) backgroundshift-=1;
       			else cdir=true;
       		}


       		int poll = win->pollInputs();

		if(poll == QUIT)
		{
			titlescreen();
			break;
		}

       		win->setImg(player1->gfx, player1->posX, player1->posY);
		win->setImg(player2->gfx, player2->posX, player2->posY);

		for(int i=0; i<ballcount; i++)
		{
			if(balls[i]->score!=-1)
			{
				if(rand()%700==1 && balls[i]->forceX<100) balls[i]->forceX *= 2;
				balls[i]->posX += balls[i]->forceX;
				balls[i]->posY += balls[i]->forceY;
				win->setImg(balls[0]->gfx, balls[i]->posX, balls[i]->posY);
			}
		}

		for(int i=0; i<ballcount; i++)
		{
			if(balls[i]->posX>sizeX
			&& balls[i]->score!=-1) { /*balls[i]->forceX*=-1; balls[i]->posX= sizeX/2;*/ balls[i]->score=-1; player1->score++; cout << "Player 1 Score: " << player1->score << endl; }

			if(balls[i]->posX-balls[i]->w<0
			&& balls[i]->score!=-1 ) { /*  balls[i]->forceX=*-1; balls[i]->posX= sizeX/2;*/  balls[i]->score=-1; player2->score++; cout << "Player 2 Score: " << player2->score << endl; }

			if(balls[i]->posY>sizeY-balls[0]->h) balls[i]->forceY*=-1;
        		if(balls[i]->posY<0) balls[i]->forceY*=-1;
		}


        	if(win->playerControls.cursorleft &&player1->posX>0) { /*cout << "left\n";*/ player1->posX-=speed; }
       		if(win->playerControls.cursorright && player1->posX < sizeX-player1->w) { /*cout << "right\n";*/ player1->posX+=speed; }
        	if(win->playerControls.cursorup  && player1->posY>0) { /*cout << "up\n";*/ player1->posY-=speed; }
        	if(win->playerControls.cursordown && player1->posY< sizeY-player1->h) { /*cout << "down\n";*/ player1->posY+=speed; }

        	if(poll==KA &&player2->posX>0) { /*cout << "left\n";*/ player2->posX-=speed; }
       		if(poll==KS && player2->posX < sizeX-player2->w) { /*cout << "right\n";*/ player2->posX+=speed; }
        	if(poll==KW && player2->posY>0) { /*cout << "up\n";*/ player2->posY-=speed; }
        	if(poll==KY && player2->posY< sizeY-player2->h) { /*cout << "down\n";*/ player2->posY+=speed; }

       		if(ai2)
        	{
            		if(rand()%10>1)
           		{
				int target = -1;
				for(int i=0; i<ballcount; i++)
				{
					if(balls[i]->forceX>0 && balls[i]->score!=-1)
					{
						target = max(target,balls[i]->posX);
					}
				}

				for(int i=0; i<ballcount; i++)
				{
					if(balls[i]->posX == target && balls[i]->forceX>0)
					{
						if(balls[i]->posY>player2->posY-10) player2->posY+=speed;
                				if (balls[i]->posY<player2->posY+10) player2->posY-=speed;
						break;
					}
				}


            		}
		}

		if(ai1)
		{
         	   	if(rand()%10>1)
            		{
          			int target = sizeX;
				for(int i=0; i<ballcount; i++)
				{
					if(balls[i]->forceX<0 && balls[i]->score!=-1)
					{
						target = min(target,balls[i]->posX);
					}
				}

				for(int i=0; i<ballcount; i++)
				{
					if(balls[i]->posX == target && balls[i]->forceX<0)
					{
						if(balls[i]->posY>player1->posY-10) player1->posY+=speed;
                				if (balls[i]->posY<player1->posY+10) player1->posY-=speed;
						break;
					}
				}
            		}

        	}


		for(int i=0; i<ballcount; i++)
		{
			if(balls[i]->colli(player1) && balls[i]->forceX<0)
        		{
               			balls[i]->forceX *= -1;
				//win->playsound();
				Mix_PlayMusic(music, 0);
        		}

        		if(balls[i]->colli(player2) && balls[i]->forceX>0)
        		{
                		balls[i]->forceX *= -1;
				//win->playsound();
				Mix_PlayMusic(music, 0);
        		}
		}

        	win->refresh();


	}
}




#ifdef main
#undef main
#endif
int main (int argc, char *argv[])
{

	if(Mix_OpenAudio(audio_rate, audio_format, audio_channels, audio_buffers))
	{
    		printf("Unable to open audio!\n");
 	}

	music = Mix_LoadMUS("sound/cannon.wav");
	Mix_PlayMusic(music, 0);

	srand(time(0));
	win = new polaris::window(sizeX,sizeY, "pong1");

	//win->loadsound("sound/cannon.wav");
	//win->playsound();
	titlescreen();

	cout << "Goodbye :)\n" << endl;
 	return 0;
}
